package control;

import model.Board;
import model.Player;

public class GameController {
	SetupController sc;
	TurnController tc;

	public static void main(String[] args) {
		new GameController().play();
		//TODO ver.2: Add option "Declare bancrupcy"
		//TODO ver.2: Add Auction
		//TODO ver.2: Add a color to Ownable - Color attribute can also be brewery or shipping(use inheritance?)(2*rent if owns complete series)
		//TODO ver.2: Add get out-of-jail-free card
		//TODO ver.2: JUnit

		//TODO Update UML. 
	}
	
	public GameController() {
		this.sc = new SetupController();
		this.tc = new TurnController();
	}

	public void play(){
		this.sc.setup();
		boolean gameOn = true;
		while(gameOn){
			//Check if game has ended
			for(int i = 0; i < Board.players.length; i++){
				int activePlayersCounter = 0;
				for(Player p : Board.players){
					if(p != null && p.isPlaying()){
						activePlayersCounter++;
					}
				}
				if(activePlayersCounter < 2){
					gameOn = false;
					break;
				}
				
				//A player takes a turn
				Player currentPlayer = Board.players[i];
				if(currentPlayer != null && currentPlayer.isPlaying()){
					this.tc.takeTurn(currentPlayer);
				}
				if(Board.cup.isSame()){
					i--;
				}
			}
		}
		declareWinner();
	}

	private void declareWinner() {
		Player winner = null;
		for(Player p : Board.players) {
			if(p.isPlaying()) { winner = p; }
		}
		if(winner != null) {
			System.out.println("The winner is " + winner.getName() + ".");
		} else {
			System.out.println("All players went bankcrupt. There is no winner.");
		}
	}
}